package com.inossem.warehousestorage_inossem.activity.inventory.chuangyou;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.activity.inventory.extre.InventoryExtre;
import com.inossem.warehousestorage_inossem.bean.inventory.chuangyou.InventoryCommitMatBean;
import com.inossem.warehousestorage_inossem.bean.inventory.chuangyou.InventoryCommitMatRequestBean;
import com.inossem.warehousestorage_inossem.bean.inventory.chuangyou.InventoryDeleteMatBean;
import com.inossem.warehousestorage_inossem.bean.inventory.chuangyou.InventorySearchCompanyBean;
import com.inossem.warehousestorage_inossem.bean.outStorage.ItemListBean;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.inventory.chuangyou.InventoryCommitMatRequest;
import com.inossem.warehousestorage_inossem.request.inventory.chuangyou.InventoryDeleteRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;
import com.inossem.warehousestorage_inossem.utils.BaseInteface;
import com.inossem.warehousestorage_inossem.utils.InventoryUtils;
import com.zhy.android.percent.support.PercentRelativeLayout;

/**
 * 物料信息
 *
 * @author 詹建宇
 * @time on 2018/4/2 11:47
 * @email jianyu.zhan@inossem.com
 */
public class InventoryMatDetailsActivity extends BaseActivity implements BaseInteface, View.OnClickListener {

    private TextView title;
    private PercentRelativeLayout chooseMat;
    private TextView codeTv;
    private TextView describeTv;
    private EditText tradingValueTv;
    private TextView tradingUnitTv;
    private PercentRelativeLayout chooseCompany;
    private TextView companyTv;
    private TextView deleteTv;
    private TextView saveTv;

    private ItemListBean bean;
    private String location_id;
    private String stocktake_id;

    private final int requestCode = 100;
    public static final int RESULT_MAT = 101;
    public static final int RESULT_COM = 102;

    private ItemListBean searchMatBodyBean;//物料
    private InventorySearchCompanyBean.BodyBean bodyBean;//供應商

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        super.onCreateImpl(savedInstanceState);
        setContentView(R.layout.cy_activity_inventory_mat_details);

        initView(this);
        initData(this);
    }

    @Override
    public void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        chooseMat = (PercentRelativeLayout) activity.findViewById(R.id.chooseMat);
        codeTv = (TextView) activity.findViewById(R.id.codeTv);
        describeTv = (TextView) activity.findViewById(R.id.describeTv);
        tradingValueTv = (EditText) activity.findViewById(R.id.tradingValueTv);
        tradingUnitTv = (TextView) activity.findViewById(R.id.tradingUnitTv);
        chooseCompany = (PercentRelativeLayout) activity.findViewById(R.id.chooseCompany);
        companyTv = (TextView) activity.findViewById(R.id.companyTv);
        deleteTv = (TextView) activity.findViewById(R.id.deleteTv);
        saveTv = (TextView) activity.findViewById(R.id.saveTv);

        chooseMat.setOnClickListener(this);
        chooseCompany.setOnClickListener(this);
        deleteTv.setOnClickListener(this);
        saveTv.setOnClickListener(this);
    }

    @Override
    public void initData(Activity activity) {
        title.setText("物料信息");
        ActivityUtils.initBackAndFunction(activity, R.string.activity_receive_result_back, new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                finish();
            }
        });
        getExtra(activity);
        if (TextUtils.isEmpty(bean.getMat_code())) {
            codeTv.setText("");
            describeTv.setText("");
            tradingValueTv.setText("");
            tradingUnitTv.setText("");
            companyTv.setText("");
        } else {
            codeTv.setText(String.valueOf(bean.getMat_code()));
            describeTv.setText(String.valueOf(bean.getMat_name()));
            tradingValueTv.setText(String.valueOf(bean.getActualSendNumber()));
            tradingUnitTv.setText(String.valueOf(bean.getUnit_name()));
            companyTv.setText(String.valueOf(bean.getSupplier_name() + "\n" + bean.getSupplier_code()));
        }
    }

    private void getExtra(Activity activity) {
        bean = (ItemListBean) activity.getIntent().getSerializableExtra(InventoryExtre.ITEMBEAN);
        location_id = activity.getIntent().getStringExtra(InventoryExtre.LOCATION_ID);
        stocktake_id = activity.getIntent().getStringExtra(InventoryExtre.STOCKTAKE_ID);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.chooseMat://选择物料
                startActivityForResult(new Intent(InventoryMatDetailsActivity.this, InventorySearchMatActivity.class).putExtra(InventoryExtre.LOCATION_ID, location_id), requestCode);
                break;
            case R.id.chooseCompany://選擇供應商
                startActivityForResult(new Intent(InventoryMatDetailsActivity.this, InventorySearchCompanyActivity.class), requestCode);
                break;
            case R.id.deleteTv://刪除
                if (!TextUtils.isEmpty(bean.getMat_code())) {
                    InventoryDeleteRequest.request(InventoryMatDetailsActivity.this, bean.getItem_id(), new GsonBeanListener<InventoryDeleteMatBean>() {
                        @Override
                        public void onSuccess(InventoryDeleteMatBean bean) {
                            Utils.middleToast(InventoryMatDetailsActivity.this, "刪除成功");
                            finish();
                        }
                    });
                }
                break;
            case R.id.saveTv://保存
                InventoryCommitMatRequestBean b = new InventoryCommitMatRequestBean();
                b.setStocktake_id(stocktake_id);
                b.setItem_id(bean.getItem_id());
                b.setMat_id(bean.getMat_id());
                b.setStocktake_qty(tradingValueTv.getText().toString().trim());
                b.setSpec_stock(null);
                b.setSpec_stock_code(null);
                b.setSpec_stock_name(null);
                b.setBatch(null);
                b.setSupplier_name(bodyBean.getSupplier_name());
                b.setSupplier_code(bodyBean.getSupplier_code());
                b.setInput_time(InventoryUtils.getTime());
                InventoryCommitMatRequest.request(InventoryMatDetailsActivity.this, b, new GsonBeanListener<InventoryCommitMatBean>() {
                    @Override
                    public void onSuccess(InventoryCommitMatBean bean) {
                        Utils.middleToast(InventoryMatDetailsActivity.this, "保存成功");
                        finish();
                    }
                });
                break;
        }
    }

    @Override
    protected void onActivityResultImpl(int requestCode, int resultCode, Intent data) {
        super.onActivityResultImpl(requestCode, resultCode, data);
        if (data == null) return;
        if (requestCode == this.requestCode) {
            if (resultCode == RESULT_MAT) {
                searchMatBodyBean = (ItemListBean) data.getSerializableExtra(InventoryExtre.SEARCH_MAT_BODY_BEAN);
                codeTv.setText(String.valueOf(searchMatBodyBean.getMat_code()));
                describeTv.setText(String.valueOf(searchMatBodyBean.getMat_name()));
                tradingUnitTv.setText(String.valueOf(searchMatBodyBean.getUnit_name()));
            } else if (resultCode == RESULT_COM) {
                bodyBean = (InventorySearchCompanyBean.BodyBean) data.getSerializableExtra(InventoryExtre.SEARCH_COM_BODY_BEAN);
                companyTv.setText(String.valueOf(bodyBean.getSupplier_name() + "\n" + bodyBean.getSupplier_code()));
            }
        }

    }

}
